package com.thread.www;

import java.util.concurrent.RecursiveTask;

/**
 * Created by 27340 on 2017/3/26.
 */
public class CountTask extends RecursiveTask<Integer>{
    private static final long serialVersionUID = 2517490453392385502L;
    private static int splitSize = 2;
    private int start,end;
    public CountTask(int start , int end){
        this.start = start;
        this.end = end;
    }

    @Override
    protected Integer compute() {
        int sum = 0;
        boolean canComputer = (end - start)<=splitSize;
        if(canComputer){
            for(int i = start;i<=end;i++){
                sum = sum+i;
            }
        }else{
            int middle = (start+end)/2;
            CountTask first = new CountTask(start , middle);
            CountTask second = new CountTask(middle + 1 , end);
            first.fork();
            second.fork();
            int firstRsult = first.join();
            int secondRsult = second.join();
            sum = firstRsult + secondRsult;
        }
        return sum;
    }
}
